define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/index' + location.search,
                    add_url: 'shop/add',
                    edit_url: 'shop/edit',
                    del_url: 'shop/del',
                    multi_url: 'shop/multi',
                    table: 'shop',
                }
            });

            var table = $("#table");

            // 修改表格搜索框的默认placeholder
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "店铺名";};

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false ,
                commonSearch: false ,
                exportTypes: ['csv','excel'] ,
                clickToSelect: false ,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'shop_id', title: __('Shop_id')},
                        {field: 'name', title: __('Name')},
                        {
                            field: 'granttime',
                            title: __('Granttime'),
                            operate:'RANGE',
                            addclass:'datetimerange',
                            formatter: Table.api.formatter.dateonly
                        },
                        {field: 'orders', title: __('Shopordernum')},
                        {
                            field: 'total',
                            title: __('Shoptotal'),
                            formatter: function (value, row, index) {
                                if (Math.floor(value) == value) {
                                    return '¥' + value + '.00';
                                }else {
                                    return '¥' + value;
                                }
                            }
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons:[
                                {
                                    name: 'shopdetail',
                                    text: __('Shopdetail'),
                                    classname: 'btn btn-success btn-dialog',
                                    url: 'shop/detail/shop_id/{shop_id}',
                                },
                                {
                                    name: 'edit',
                                    text: __('Edit'),
                                    classname: 'btn btn-info btn-dialog',
                                    url: 'shop/edit',
                                },
                                {
                                    name: 'del',
                                    text: __('Del'),
                                    classname: 'btn btn-primary btn-ajax',
                                    url: 'shop/del',
                                    confirm: '确认删除?',
                                    refresh: true,
                                },
                            ],
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);

                                // 隐藏编辑和删除按钮
                                // $(table).data("operate-edit", null);
                                // $(table).data("operate-del", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});